package cn.wolfcode.plus.web.controller;

import cn.wolfcode.plus.domain.Department;
import cn.wolfcode.plus.qo.PageResult;
import cn.wolfcode.plus.qo.QueryObject;
import cn.wolfcode.plus.service.IDepartmentService;
import cn.wolfcode.plus.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * Author wyy
 * Date 2021/2/10 11:43
 **/
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiredPermission(name="部门列表",expression="department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageResult<Department> result = departmentService.query(qo);
        model.addAttribute("result",result);
        return "department/list";
    }

    @RequiredPermission(name="部门删除",expression="department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        //System.out.println(id);
        departmentService.removeById(id);
        return "redirect:/department/list";
    }

    @RequiredPermission(name="部门新增/编辑",expression="department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if(department.getId()==null){
            //执行增加操作
            departmentService.save(department);
        }else{
            //执行修改操作
            departmentService.updateById(department);
        }
        return "redirect:/department/list";
    }

}
